#include "oscilloscopewidget.h"


OscilloscopeWidget::OscilloscopeWidget(QGraphicsWidget* parent)
    : QGraphicsWidget(parent)
{
    setAcceptedMouseButtons(Qt::LeftButton);
    setAcceptDrops(true);
    setCursor(Qt::OpenHandCursor);

    m_nameLabel = new QLabel;
    m_curveWidget = new CurveWidget;

    QGraphicsProxyWidget* proxyWidget1 = new QGraphicsProxyWidget;
    proxyWidget1->setWidget(m_nameLabel);

    QGraphicsLinearLayout * hLayout = new QGraphicsLinearLayout (Qt::Horizontal);
    hLayout->setContentsMargins(0,0,0,0);
    hLayout->addItem(proxyWidget1);
    hLayout->addItem(m_curveWidget);
    setLayout(hLayout);


}


void OscilloscopeWidget::setCurveName(QString name, QFont font)
{
    m_nameLabel->setText(name);
    m_nameLabel->setFont(font);
}

void OscilloscopeWidget::setLabelWidth(int width)
{
    m_nameLabel->setFixedWidth(width);
}

void OscilloscopeWidget::setColor(QColor backgroundColor, QColor foregroundColor)
{
    m_curveWidget->setColors(backgroundColor, foregroundColor);
}

void OscilloscopeWidget::setCurvePath(QPainterPath *path)
{
    m_curveWidget->setPainterPath(path);
}

void OscilloscopeWidget::setPos(QPoint point)
{
    m_pos = point;
}

void OscilloscopeWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    m_curveWidget->resize(size());
}

void OscilloscopeWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event)
    setCursor(Qt::ClosedHandCursor);
}

void OscilloscopeWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
            .length() < QApplication::startDragDistance()) {
        return;
    }

    QDrag *drag = new QDrag(event->widget());
    QMimeData *mime = new QMimeData;
    drag->setMimeData(mime);

    QPixmap pixmap = QPixmap::grabWidget(event->widget());
    qDebug()<<this->pos() << size();
    QRect rect(m_pos.x(), m_pos.y(), size().width(), size().height());
    QPixmap newPixmap = pixmap.copy(rect);

    drag->setPixmap(newPixmap);
    drag->setHotSpot(event->pos().toPoint());
    pixmap.setMask(pixmap.createHeuristicMask());
    drag->exec();
    setCursor(Qt::OpenHandCursor);
}

void OscilloscopeWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(Qt::OpenHandCursor);
}

